﻿using System;
using System.Linq;
using System.Web.Mvc;
using IloveRSS.Models;
using IloveRSS.Core;

namespace IloveRSS.Controllers
{
    public class SearchController : Controller
    {
        //
        // GET: /Search/
        private DataManager dataManager;

        public SearchController(DataManager dm)
        {
            dataManager = dm;
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Index()
        {
            ViewBag.TagCloud = dataManager.GetAllTags();
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Index(String searchText)
        {
            if (TextConverter.TagsAreNotValid(searchText))
            {
                ModelState.AddModelError("searchText", "Поисковой запрос пуст либо содержит недопустимые символы");
                return View();
            }
            ViewBag.SearchQuery = searchText;
            ViewBag.SearchResult = dataManager.FindChannelsByTags(TextConverter.ConvertToTagList(searchText).ToList<String>());
            return View();
        }

        public ActionResult TagCloud(string tag)
        {
            ViewBag.SearchResult = dataManager.FindChannelsByTags(TextConverter.ConvertToTagList(tag.Trim()).ToList<String>());
            return View();
        }
    }
}
